"""
文件名:Base.py
作用: 对selenium进行二次封装
    1.打开浏览器(单独封装)
    2.输入网址
    3.元素定位
    4.元素操作
    5.控制操作
"""
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time



# 1.打开浏览器
def open_browser(browser='Chrome'):
    """
    打开浏览器:通过浏览器名称,打开对应的浏览器
    字符串.capitalize():python中字符串首字母大写
    :param browser:浏览器名称
    :return: driver
    """
    if browser.capitalize() == 'Chrome':
        return webdriver.Chrome()  # 打开谷歌浏览器
    elif browser.capitalize() == 'Firefox':
        return webdriver.Firefox()  # 打开火狐浏览器
    elif browser.capitalize() == 'Edge':
        return webdriver.Edge()  # 打开Edge浏览器
    else:
        print('请输入正确的浏览器名称,如: Chrome,Firefox,Edge')
        return None


class Base(object):
    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        """
        输入网址
        :param url: 测试的网址
        :return:
        """
        try:
            self.driver.get(url)
            self.driver.maximize_window()
        except:
            print('浏览器打开失败,无法输入网址')

    def close_browser(self):
        """
        关闭浏览器
        :return:
        """
        try:
            self.driver.quit()
        except:
            print('浏览器打开失败,无法执行浏览器关闭操作')

    def find_element(self, locator: tuple, timeout: int = 10):
        """
        定位单个元素,如果元素存在,返回元素本身;反之返回False
        使用EC模块中的元素定位方法结合显示等待一起使用

        locator: tuple 表示给函数的形参指定数据类型
        :param: locator: 定位器,数据类型是元组
        :return:
        """
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))
        except:
            print(f'元素{locator}未找到')
            return False

    def find_elemts(self, locator: tuple, timeout: int = 10):
        """
        元素定位的复数形式,如果存在,返回列表,不存在,则返回False
        :param locator: 定位器,元组
        :param timeout: 超时时间
        :return:
        """
        try:
            return WebDriverWait(self.driver,timeout).until(EC.presence_of_all_elements_located(locator))
        except:
            print(f'元素{locator}未找到')
            return False

    def send_keys(self,locator:tuple,text):
        """
        元素输入
        :param locator: 定位器
        :param text: 输入内容
        :return:
        """
        try:
            element = self.find_element(locator)
            element.send_keys(text)
        except:
            print(f'元素{locator}未找到,无法输入')

    def click(self,locator):
        """
        元素点击
        :param locator: 定位器
        :return:
        """
        try:
            element = self.find_element(locator)
            element.click()
        except:
            print(f'元素{locator}未找到,无法点击')

    def get_element_text(self,locator):
        """
        获取属性文本值
        方法:属性.text
        :param locator: 元素定位器
        :return:
        """
        try:
            element = self.find_element(locator)
            return element.text
        except:
            print(f'元素{locator}未找到,无法获取元素文本值')

    def get_element_value(self,locator):
        """
        获取元素value属性值
        :param locator:
        :return:
        """
        try:
            element = self.find_element(locator)
            return element.get_attribute('value')
        except:
            print(f'元素{locator}未找到,无法获取元素value属性值')

    def back(self):
        """
        浏览器后退
        :return:
        """
        try:
            self.driver.back()
        except:
            print('浏览器未打开,无法执行浏览器后退操作')

    def refresh(self):
        """
        刷新浏览器
        :return:
        """
        try:
            self.driver.refresh()
        except:
            print('浏览器未打开,无法执行浏览器刷新操作')

    def select(self,locator,index:int=0):
        """
        下拉菜单操作
        :param locator: 元素定位器
        :param index: 下拉菜单中选项索引号
        :return:
        """
        try:
            element=self.find_element(locator)
            return Select(element).select_by_index(index)
        except:
            print(f'元素{locator}未找到,无法操作下拉菜单')



if __name__ == '__main__':
    base = Base(open_browser('chrome'))
    base.get_url('http://localhost:8080/ecshop/user.php?act=register')
    # send_locator=(By.ID,'kw')
    # click_loctor=(By.ID,'su')
    # setting_locator=(By.ID,'s-usersetting-top')
    # # base.send_keys(send_locator,'间谍过家家')
    # # base.click(click_loctor)
    # print(base.get_element_value(click_loctor))
    # print(base.get_element_text(setting_locator))
    select_loc=(By.NAME,'sel_question')
    base.select(select_loc,1)
    time.sleep(5)
    base.close_browser()

